package com.ugoodtech.mdcc.client.controller;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.controller.ResponseObject.KpiDataResponse;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.core.domain.StationLeanPaintSatisfactionReport.OperationalDataAnalysis;
import com.ugoodtech.mdcc.core.domain.StationLeanPaintSatisfactionReport.OperationalDataAnalysisTemp;
import com.ugoodtech.mdcc.core.domain.StationLeanPaintSatisfactionReport.QOperationalDataAnalysis;
import com.ugoodtech.mdcc.core.domain.StationLeanPaintSatisfactionReport.QOperationalDataAnalysisTemp;
import com.ugoodtech.mdcc.core.domain.StationRegularServiceReport;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.OperationalDataAnalysisRepository;
import com.ugoodtech.mdcc.core.repository.OperationalDataAnalysisTempRepository;
import com.ugoodtech.mdcc.core.repository.StationRegularServiceReportRepository;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/getKpiData")
public class GetKpiDataController {

    @Autowired
    private OperationalDataAnalysisRepository operationalDataAnalysisRepository;

    @Autowired
    private OperationalDataAnalysisTempRepository operationalDataAnalysisTempRepository;

    @Autowired
    private UserRepository userRepository;

    private static Integer[] index = new Integer[7];

    static {
        for (int i = 0; i < index.length; i++) {
            index[i] = i;
        }
    }

    @ApiOperation("移动")
    @RequestMapping(value = "changeKpiData", method = RequestMethod.GET)
    public JsonResponse changeKpiData(Principal principal,
                                      @ApiParam("报告ID") @RequestParam(value = "reportId", required = false) Long reportId,
                                      @ApiParam("维修站ID") @RequestParam(value = "stationId") Long stationId,
                                      @ApiParam("前移或者后移") @RequestParam(value = "upDown") boolean upDown,
                                      @ApiParam("移动位数") @RequestParam(value = "moveIndex") Integer moveIndex) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        if (reportId == null || reportId == 0 || moveIndex >= 7) {
            List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setIndexId(i);
                kpiDataResponseList.add(kpiDataResponse);
            }
            return JsonResponse.successResponseWithData(kpiDataResponseList);
        }

        QOperationalDataAnalysis qOperationalDataAnalysis = QOperationalDataAnalysis.operationalDataAnalysis;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qOperationalDataAnalysis.deleted.eq(false));
        builder.and(qOperationalDataAnalysis.enabled.eq(true));
        builder.and(qOperationalDataAnalysis.reportId.eq(reportId));
        builder.and(qOperationalDataAnalysis.stationId.eq(stationId));
        builder.and(qOperationalDataAnalysis.indexId.in(index));
        List<OperationalDataAnalysis> operationalDataAnalysisList = (List<OperationalDataAnalysis>) operationalDataAnalysisRepository.findAll(builder);
        List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
        for (OperationalDataAnalysis data : operationalDataAnalysisList) {
            KpiDataResponse kpiDataResponse = new KpiDataResponse(data);
            kpiDataResponseList.add(kpiDataResponse);
        }

        for (int i = 0; i < moveIndex; i++) {
            KpiDataResponse k0 = kpiDataResponseList.get(0);
            k0.setIndexId(1);
            KpiDataResponse k1 = kpiDataResponseList.get(1);
            k1.setIndexId(2);
            KpiDataResponse k2 = kpiDataResponseList.get(2);
            k2.setIndexId(3);
            KpiDataResponse k3 = kpiDataResponseList.get(3);
            k3.setIndexId(4);
            KpiDataResponse k4 = kpiDataResponseList.get(4);
            k4.setIndexId(5);
            KpiDataResponse k5 = kpiDataResponseList.get(5);
            k5.setIndexId(6);
            KpiDataResponse k6 = kpiDataResponseList.get(6);
            if (upDown == true) {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setReportId(k0.getReportId());
                kpiDataResponse.setStationId(k0.getStationId());
                kpiDataResponse.setId(k0.getId());
                k0.setId(k1.getId());
                k1.setId(k2.getId());
                k2.setId(k3.getId());
                k3.setId(k4.getId());
                k4.setId(k5.getId());
                k5.setId(k6.getId());
                kpiDataResponseList.set(0, kpiDataResponse);
                kpiDataResponseList.set(1, k0);
                kpiDataResponseList.set(2, k1);
                kpiDataResponseList.set(3, k2);
                kpiDataResponseList.set(4, k3);
                kpiDataResponseList.set(5, k4);
                kpiDataResponseList.set(6, k5);
            } else {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setReportId(k6.getReportId());
                kpiDataResponse.setStationId(k6.getStationId());
                kpiDataResponse.setId(k6.getId());
                kpiDataResponse.setIndexId(6);

                k6.setId(k5.getId());
                k6.setIndexId(5);
                k5.setId(k4.getId());
                k5.setIndexId(4);
                k4.setId(k3.getId());
                k4.setIndexId(3);
                k3.setId(k2.getId());
                k3.setIndexId(2);
                k2.setId(k1.getId());
                k2.setIndexId(1);
                k1.setId(k0.getId());
                k1.setIndexId(0);

                kpiDataResponseList.set(6, kpiDataResponse);
                kpiDataResponseList.set(5, k6);
                kpiDataResponseList.set(4, k5);
                kpiDataResponseList.set(3, k4);
                kpiDataResponseList.set(2, k3);
                kpiDataResponseList.set(1, k2);
                kpiDataResponseList.set(0, k1);
            }
        }
        return JsonResponse.successResponseWithData(kpiDataResponseList);
    }

    @ApiOperation("获取提交的精益板喷KPI数据")
    @RequestMapping(value = "getData", method = RequestMethod.GET)
    public JsonResponse getKpiData(@ApiParam("报告ID") @RequestParam(value = "reportId", required = false) Long reportId,
                                   @ApiParam("维修站ID") @RequestParam(value = "stationId") Long stationId) {
        if (reportId == null || reportId == 0) {
            List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setIndexId(i);
                kpiDataResponseList.add(kpiDataResponse);
            }
            return JsonResponse.successResponseWithData(kpiDataResponseList);
        }
        QOperationalDataAnalysis qOperationalDataAnalysis = QOperationalDataAnalysis.operationalDataAnalysis;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qOperationalDataAnalysis.deleted.eq(false));
        builder.and(qOperationalDataAnalysis.enabled.eq(true));
        builder.and(qOperationalDataAnalysis.reportId.eq(reportId));
        builder.and(qOperationalDataAnalysis.stationId.eq(stationId));
        builder.and(qOperationalDataAnalysis.indexId.in(index));
        List<OperationalDataAnalysis> operationalDataAnalysisList = (List<OperationalDataAnalysis>) operationalDataAnalysisRepository.findAll(builder, new Sort(Sort.Direction.ASC, "indexId"));
        if (operationalDataAnalysisList == null || operationalDataAnalysisList.size() <= 0) {
            List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setIndexId(i);
                kpiDataResponseList.add(kpiDataResponse);
            }
            return JsonResponse.successResponseWithData(kpiDataResponseList);
        }
        List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
        for (OperationalDataAnalysis data : operationalDataAnalysisList) {
            KpiDataResponse kpiDataResponse = new KpiDataResponse(data);
            kpiDataResponseList.add(kpiDataResponse);
        }
        return JsonResponse.successResponseWithData(kpiDataResponseList);
    }

    @ApiOperation("获取提交的精益板喷KPI数据")
    @RequestMapping(value = "getData_temp", method = RequestMethod.GET)
    public JsonResponse getKpiData_temp(@ApiParam("报告ID") @RequestParam(value = "reportId", required = false) Long reportId,
                                        @ApiParam("维修站ID") @RequestParam(value = "stationId") Long stationId) {
        if (reportId == null || reportId == 0) {
            List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setIndexId(i);
                kpiDataResponseList.add(kpiDataResponse);
            }
            return JsonResponse.successResponseWithData(kpiDataResponseList);
        }
        QOperationalDataAnalysisTemp qOperationalDataAnalysisTemp = QOperationalDataAnalysisTemp.operationalDataAnalysisTemp;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qOperationalDataAnalysisTemp.deleted.eq(false));
        builder.and(qOperationalDataAnalysisTemp.enabled.eq(true));
        builder.and(qOperationalDataAnalysisTemp.reportId.eq(reportId));
        builder.and(qOperationalDataAnalysisTemp.stationId.eq(stationId));
        builder.and(qOperationalDataAnalysisTemp.indexId.in(index));
        List<OperationalDataAnalysisTemp> operationalDataAnalysisList = (List<OperationalDataAnalysisTemp>) operationalDataAnalysisTempRepository.findAll(builder, new Sort(Sort.Direction.ASC, "indexId"));
        if (operationalDataAnalysisList == null || operationalDataAnalysisList.size() <= 0) {
            List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                KpiDataResponse kpiDataResponse = new KpiDataResponse();
                kpiDataResponse.setIndexId(i);
                kpiDataResponseList.add(kpiDataResponse);
            }
            return JsonResponse.successResponseWithData(kpiDataResponseList);
        }
        List<KpiDataResponse> kpiDataResponseList = new ArrayList<>();
        for (OperationalDataAnalysisTemp data : operationalDataAnalysisList) {
            KpiDataResponse kpiDataResponse = new KpiDataResponse(data);
            kpiDataResponseList.add(kpiDataResponse);
        }
        return JsonResponse.successResponseWithData(kpiDataResponseList);
    }

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @ApiOperation("获取提交的精益板喷KPI数据")
    @RequestMapping(value = "getTest", method = RequestMethod.GET)
    public JsonResponse getTest() {
        StationRegularServiceReport regularServiceReport = stationRegularServiceReportRepository.selectQuestionOther(258L, 155L);
        return JsonResponse.successResponseWithData(regularServiceReport);
    }


}
